<template>
  <div class="container">
<el-form :model="tagInfo" label-width="100px" :rules="rules" ref="ruleForm">

    <el-form-item label="所属学科"  v-if="!$route.query.id&&!$route.query.name" >
      <el-select v-model="tagInfo.subjectID" placeholder="请选择活动区域" >
        <el-option v-for="(item) in subject" :key="item.value"  :label="item.label" :value="item.value"></el-option>

      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" prop="tagName">
    <el-input v-model="tagInfo.tagName" style="width:218px"></el-input>
  </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer" >
    <el-button @click="$emit('close',false)">取 消</el-button>
    <el-button type="primary" @click="dialogTagVisible">确 定</el-button>
  </div>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/tags'
export default {
  name: 'addTags',
  props: {
    subject: {
      type: Array,
      required: true
    }
  },
  components: {

  },
  data () {
    return {
      rules: {
        tagName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }]
      },
      tagInfo: {
        subjectID: '',
        tagName: ''

      }
    }
  },
  created () {

  },
  methods: {
    dialogTagVisible () {
      this.$refs.ruleForm.validate(async (valid) => {
        // console.log(valid)
        if (valid) {
          // 验证通过
          if (!this.$route.query.id && !this.$route.query.name) {
            const { data: res } = await add(this.tagInfo)
            console.log(res)
          } else {
            this.tagInfo.subjectID = +this.$route.query.id
            await add(this.tagInfo)
          }
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.tagInfo.subjectID = ''
          this.tagInfo.tagName = ''
          this.$emit('updateTag')
          this.$emit('close', false)
        }
      })
    },

    clearform () {
      // this.$refs.ruleForm.resetFields()
      this.tagInfo.subjectID = ''
      this.tagInfo.tagName = ''
    }
  }
}
</script>

<style lang="less" scoped>
.dialog-footer{
  text-align: center;
}
</style>
